var scene, //场景
  camera, //摄像机
  renderer, //渲染
  container, //容器
  aspect, //摄像机视锥体长宽比
  fov, //。。。。。。垂直视野角度
  plane, //。。。。。。近端面
  far, //。。。。。。远端面
  mouseX, //鼠标位置
  mouseY,
  windowHalfX, //平面一半
  windowHalfY,
  geometry,
  starStuff,
  materialOptions,
  stars;

init();
animate();
// console.log(animate);

function init() {
  container = document.getElementById(`canvas`);

  mouseX = 0;
  mouseY = 0;

  aspect = window.innerWidth / window.innerHeight;
  fov = 40;
  plane = 1;
  far = 800;

  windowHalfX = window.innerWidth / 2;
  windowHalfY = window.innerHeight / 2;

  camera = new THREE.PerspectiveCamera(fov, aspect, plane, far); //确定视锥范围，在范围内才能被渲染

  camera.position.z = far / 2;
  // console.log(camera);
  scene = new THREE.Scene({ antialias: true }); //抗锯齿
  scene.fog = new THREE.FogExp2(0x1b1b1b, 0.0001); //雾化效果

  starForge();
  renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
  renderer.setPixelRatio(window.devicePixelRatio ? window.devicePixelRatio : 1); //设置设备像素比
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.autoClear = false; //re得到的像素数据叠加
  renderer.setClearColor(0x000000, 0.0);
  // console.log(renderer)
  // console.log(renderer.domElement)
  container.appendChild(renderer.domElement);

  document.addEventListener("mousemove", onMouseMove, false);
}

function animate() {
  //动画
  requestAnimationFrame(animate);
  render();
}

function render() {
  //渲染
  camera.position.x += (mouseX - camera.position.x) * 0.005;
  camera.position.y += (-mouseY - camera.position.y) * 0.005;
  camera.lookAt(scene.position);

  renderer.render(scene, camera);
}

function starForge() {
  var amount = 45000;
  geometry = new THREE.SphereGeometry(1000, 100, 50); //创建三维球

  materialOptions = {
    //材质
    color: new THREE.Color(0xffffff),
    size: 1.1,
    opacity: 0.8,
  };

  starStuff = new THREE.PointsMaterial(materialOptions);
  // console.log(starStuff);

  for (var i = 0; i < amount; i++) {
    var item = new THREE.Vector3();
    item.x = Math.random() * 2000 - 1000;
    item.y = Math.random() * 2000 - 1000;
    item.z = Math.random() * 2000 - 1000;

    // console.log(item);
    geometry.vertices.push(item); //放定点
  }
  // console.log(geometry);
  stars = new THREE.Points(geometry, starStuff);
  scene.add(stars);

  console.log(scene);
}

function onMouseMove(e) {
  mouseX = e.clientX - windowHalfX;
  mouseY = e.clientY - windowHalfY;
}
